import { ml_hideLoading, ml_showLoading, ml_showToast, ml_Toast } from '../../../utils/asyncWX'
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sneg: true, //只看需求
    index1: null,
    index2: null,
    time1: 0,
    choise_time_String: ['中午', '傍晚', '晚上'],
    choise_time: ['中午 (12:30~13:30)', '傍晚 (17:30~18:30)', '晚上 (21:00~22:00)'],
    picker1: ['普通配送', '急件配送'],
    picker2: ['快递代拿',  '外卖代拿', '校园代购'],
    rider_inf: null,
    all_list: [],
    collection_list: []
  },

  PickerChange1(e) {
    let all_list = this.data.all_list;
    let index = e.detail.value;
    let collection = [];
    for (let i = 0; i < all_list.length; i++) {
      if (this.data.picker1[index] == all_list[i].pay_orderinf.dispatchin_type) {
        collection.push(all_list[i]);
      }
    }
    this.setData({
      index1: index,
      collection_list: collection
    })
  },
  PickerChange2(e) {
    let all_list = this.data.all_list;
    let index = e.detail.value;
    let collection = [];
    for (let i = 0; i < all_list.length; i++) {
      if (this.data.picker2[index] == all_list[i].pay_orderinf.order_typepack) {
        if (all_list[i].pay_orderinf.dispatchin_type == '普通配送') {
          collection.push(all_list[i]);
        }
      }
    }
    this.setData({
      index2: e.detail.value,
      collection_list: collection
    })
  },

  PickerChange4: function (e) {
    let all_list = this.data.all_list;
    let index = e.detail.value;
    let collection = [];
    for (let i = 0; i < all_list.length; i++) {
      if (this.data.choise_time[index] == all_list[i].pay_orderinf.choise_time) {
        if (all_list[i].pay_orderinf.dispatchin_type == '普通配送') {
          collection.push(all_list[i]);
        }
      }
    }
    this.setData({
      time1: e.detail.value,
      collection_list: collection
    })
  },

  SetShadow() { this.setData({ sneg: !this.data.sneg }); },

  //获取订单消息
  getorder_list() {
    let rider_infs = app.globalData.rider_inf;
    console.log(rider_infs);
    ml_showLoading();
    wx.cloud.callFunction({
      name: 'getorder_list', data: {
        type: 'getdata',
        rider_inf: rider_infs
      }
    }).then(res => {
      console.log(res)
      ml_hideLoading();
      app.globalData['rider_inf'] = res.result.rider_inf.data[0];
      let order_wait_inf = res.result.order_wait_inf.data.reverse();
      let rider_inf = res.result.rider_inf.data[0];
      rider_inf['rider_money'] = Math.round(rider_inf.rider_money * 100) / 100
      if (res.result.rider_inf.data.length == 0) {
        this.close_yuem();
      } else {
        this.setData({
          all_list: order_wait_inf,
          collection_list: order_wait_inf,
          rider_inf: rider_inf
        })
      }

    })
  },

  close_yuem() {
    wx.setStorageSync('rider_inf_data', '');
    wx.setStorageSync('rider_inf_reb', '');
    app.globalData['rider_inf'] = null;
    wx.switchTab({
      url: '/pages/home_page/home_page',
    })
  },

  order_updata(e) {
    let rider_infs = app.globalData.rider_inf;
    let order_inf = e.currentTarget.dataset.item;
    wx.cloud.callFunction({
      name: 'getorder_list', data: {
        type: 'update_rider',
        id: order_inf._id,
        rider_inf: rider_infs
      }
    }).then(res => {
      console.log(res);
      this.getorder_list();
      if (res.result.rider_inf.stats.updated == 1) {
        ml_showToast('接单成功');
      } else {
        ml_Toast('接单失败，请重新尝试！');
      }
    })
  },
  order_pay(e) {
    let order_inf = e.currentTarget.dataset.item;
    console.log(order_inf);
    let rider_inf = app.globalData['rider_inf'];
    let timeString = (new Date()).getTime();
    let datas = {
      _openid: order_inf._openid,
      pay_time: order_inf.pay_time,         //支付时间
      order_number: order_inf.order_number,              //订单号
      pay_orderinf: order_inf.pay_orderinf,          //订单详情
      need_pay: Number(order_inf.pay_aftermoney),          // 需支付金额
      pay_money: Number(order_inf.pay_money),           //实付金额
      pay_store_inf: { 'pay_store_weight': order_inf.pay_orderinf.order_sizes, 'pay_store_inf': order_inf.pay_orderinf.order_sizes },                          //物品信息
      pay_courier_inf: { '_id': rider_inf._id, 'rider_name': rider_inf.rider_name, 'rider_phone': rider_inf.rider_phone, 'rider_id': rider_inf.rider_id, 'rider_not_name': rider_inf.rider_not_name, 'rider_not_phone': rider_inf.rider_not_phone, rider_ratio: rider_inf.rider_ratio },            //快递员信息
      rider_id: rider_inf.rider_id,              //快递员识别号
      coupon_inf: order_inf.coupon_inf,              // 优惠卷信息
      order_code: 0,    //订单状态码
      order_state: [{ 'state': '已支付', 'state_title': '骑手已接单，开始运送', 'pay_time': timeString }],                                 //订单状态
      ifover_order: false                            //是否签收
    };
    ml_showLoading('加载中....')
    wx.cloud.callFunction({
      name: 'getopenid', data: {
        type: 'daisho_update_order',
        datas: datas,
        delect_id: order_inf._id
      }
    }).then(res => {
      if (res.result.rider_inf.stats.updated == 1) {
        ml_showToast('接单成功！');
        this.getorder_list();
      } else {
        ml_Toast('记录数据库失败，请联系客服人员！');
      }
      console.log(res);
    }).catch(res => {
      console.log(res);
      ml_Toast('接单失败，请重新尝试')
    })
  },

  Lookuesrimage() {
    wx.navigateTo({
      url: '/packageA/pages/Collection/Collection',
    })
  },
  Lookuserinf(e) {
    app.globalData.money = e.currentTarget.dataset.money;
    wx.navigateTo({
      url: '/packageA/pages/cashout/cashout',
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getorder_list();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getorder_list();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})